<template>
  <div class="app">
    <el-row class="header">
      <el-col class="title">
        <span>欢迎来到</span>
        <span>后台管理系统</span>
      </el-col>
    </el-row>
    <el-row class="content">
      <el-form
        label-width="20"
        class="form"
        :rules="rules"
        ref="form"
        :model="formData"
      >
        <el-form-item label="用户名" class="formItem" prop="username">
          <el-input class="formInput" v-model="formData.username" />
        </el-form-item>
        <el-form-item label="密码" class="formItem" prop="password">
          <el-input class="formInput" v-model="formData.password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">登录</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- <div class="userBox">
        <label for="username" class="labelUser">* 用户名</label>
        <input
          id="username"
          class="username"
          v-model="inputValue"
          placeholder="请输入用户名"
          placeholder-class="input-placeholder"
        />
      </div>
      <div class="passBox">
        <label for="password" class="labelPass">* 密码</label>
        <input
          id="password"
          class="password"
          v-model="inputValue"
          placeholder="请输入密码"
          placeholder-class="input-placeholder"
        />
      </div> -->
      <!-- <div class="btnBox">
        <button class="loginBtn">登录</button>
        <div class="box"></div>
        <button class="loginBtn">重置</button>
      </div> -->
    </el-row>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  name: 'loginIndex',
  data () {
    return {
      formData: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: ['blur', 'change']
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  methods: {
    ...mapActions('user', ['adminLogin']),
    async onSubmit () {
      try {
        await this.$refs.form.validate()
        await this.adminLogin(this.formData)
        this.$router.push({ path: '/layout/dashboard' })
        this.$message.success({ message: '登录成功', duration: 1000 })
      } catch (error) {
        this.$message.warning({
          message: '请输入正确的用户名或密码',
          duration: 1300
        })
      }
    },
    onReset () {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss">
.app {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  background-image: url("@/assets/login-bg.svg");
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}
.header {
  height: 80px;
  width: 425px;
  text-align: center;
  border-radius: 4px 4px 0 0;
  line-height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #727cf5;
  color: #fff;

  .title {
    margin: 0;
    font-size: 25px;
    line-height: 80px;
  }
}
.content {
  position: relative;
  width: 425px;
  height: 300px;
  border-radius: 0 0 4px 4px;
  box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.2);
  background-color: #fff;
  display: flex;
  align-items: center;
  flex-direction: column;

  form {
    width: 320px;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: start;
  }
}
.el-form-item {
  margin-bottom: 0 !important;
}
.el-form-item:first-child,
.el-form-item:last-child {
  margin-top: 20px;
  text-align: center;
}
.el-form-item:nth-child(2) {
  margin-top: 10px;
}
</style>
